package com.gy.assistant.ui.grouplist;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMGroupListQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMGroup;
import im.zego.zim.entity.ZIMGroupFullInfo;
import im.zego.zim.entity.ZIMGroupInfo;
import im.zego.zim.entity.ZIMGroupOperatedInfo;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zim.enums.ZIMGroupEvent;
import im.zego.zim.enums.ZIMGroupState;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.chat.group.ZIMGroupChatActivity;

public class ZIMGroupInfoListFragment extends Fragment {

    private static final String TAG = "ZIMConversationFragment";

    private GroupInfoListAdapter groupInfoListAdapter = new GroupInfoListAdapter();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_group_info_list, container, false);

        RecyclerView groupInfoListView = rootView.findViewById(R.id.group_info_list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        groupInfoListView.setLayoutManager(linearLayoutManager);
        groupInfoListView.setAdapter(groupInfoListAdapter);
        groupInfoListView.setItemAnimator(new DefaultItemAnimator());

        setHasOptionsMenu(true);
        setGroupEventHandler();
        setItemClickEvent();

        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        SDKManager.sharedInstance().queryGroupList(new ZIMGroupListQueriedCallback() {
            @Override
            public void onGroupListQueried(ArrayList<ZIMGroup> groupList, ZIMError errorInfo) {

                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(getActivity(), "Failed to query group, error:" + errorInfo.code, Toast.LENGTH_SHORT).show();
                    return;
                }
                List<ZIMGroupInfo> infos = new ArrayList<>();
                for (ZIMGroup group : groupList) {
                    infos.add(group.baseInfo);
                }

                groupInfoListAdapter.addAll(infos);
                groupInfoListAdapter.notifyDataSetChanged();
            }
        });
    }

    private void setItemClickEvent() {
        groupInfoListAdapter.setOnItemClickListener(new GroupInfoListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ZIMGroupInfo groupInfo) {

                ZIMGroupChatActivity.actionStart(getActivity(), groupInfo.groupID, groupInfo.groupName,"");
            }
        });
    }

    private void setGroupEventHandler() {
        SDKManager.sharedInstance().setGroupStateChangedCallback(new SDKManager.OnGroupStateChangedCallback() {
            @Override
            public void onGroupStateChanged(ZIM zim, ZIMGroupState state, ZIMGroupEvent event, ZIMGroupOperatedInfo operatedInfo, ZIMGroupFullInfo groupInfo) {
                switch (state) {
                    case ENTER:
                        groupInfoListAdapter.add(groupInfo.baseInfo);
                        break;
                    case QUIT:
                        groupInfoListAdapter.delete(groupInfo.baseInfo);
                        break;
                }
                groupInfoListAdapter.notifyDataSetChanged();
            }
        });

        SDKManager.sharedInstance().addGroupNameUpdateCallback(groupNameUpdateCallback);
    }

    private SDKManager.OnGroupNameUpdateCallback groupNameUpdateCallback = new SDKManager.OnGroupNameUpdateCallback() {
        @Override
        public void onGroupNameUpdated(ZIM zim, String groupName, ZIMGroupOperatedInfo operatedInfo, String groupID) {
            ZIMGroupInfo groupInfo = groupInfoListAdapter.query(groupID);
            groupInfo.groupName = groupName;
            groupInfoListAdapter.update(groupInfo);
            groupInfoListAdapter.notifyDataSetChanged();
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        SDKManager.sharedInstance().removeGroupNameUpdateCallback(groupNameUpdateCallback);
    }
}
